Funcion para manejar datos missing
## # A tibble: 312 × 4
## `FISCALIA DISTRITO` Año Delito Casos
## <chr> <fct> <chr> <dbl>
## 1 Aguadilla 2020 Art2.8 29
## 2 Aguadilla 2020 Art3.1 304
## 3 Aguadilla 2020 Art3.2 94
## 4 Aguadilla 2020 Art3.3 142
## 5 Aguadilla 2020 Art3.4 2
## 6 Aguadilla 2020 Art3.5 2
## 7 Aibonito 2020 Art2.8 22
## 8 Aibonito 2020 Art3.1 216
## 9 Aibonito 2020 Art3.2 49
## 10 Aibonito 2020 Art3.3 75
## # … with 302 more rows
# Crear un dataframe con las coordenadas de las fiscalías policiacas
distritos_fiscales <- sf::st_read(paste0(path2, "distritos_fiscales.json"))
## Reading layer `distritos_fiscales' from data source
## `/Users/felixbaez822/Desktop/Estadisticas-Sobre-Violencia-de-Genero-en-Puerto-Rico/data/mapas/distritos_fiscales.json'
## using driver `GeoJSON'
## Simple feature collection with 78 features and 4 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -67.27135 ymin: 17.92688 xmax: -65.24442 ymax: 18.51576
## Geodetic CRS: WGS 84
as.tibble(distritos_fiscales)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## # A tibble: 78 × 5
## STATE COUNTY NAME GROUP geometry
## <chr> <chr> <chr> <chr> <POLYGON [°]>
## 1 72 071 Isabela Aguadilla ((-67.10328 18.51343, -67.10339 18.51453…
## 2 72 005 Aguadilla Aguadilla ((-67.10328 18.51343, -67.10281 18.51291…
## 3 72 013 Arecibo Arecibo ((-66.58678 18.48495, -66.58683 18.48464…
## 4 72 065 Hatillo Arecibo ((-66.76483 18.48407, -66.76598 18.4819,…
## 5 72 115 Quebradillas Arecibo ((-66.90143 18.48455, -66.90138 18.48328…
## 6 72 027 Camuy Arecibo ((-66.83667 18.4877, -66.83656 18.48737,…
## 7 72 017 Barceloneta Arecibo ((-66.58683 18.48464, -66.58678 18.48495…
## 8 72 145 Vega Baja Bayamón ((-66.34966 18.48634, -66.34942 18.47706…
## 9 72 143 Vega Alta Bayamón ((-66.31503 18.47468, -66.31543 18.47068…
## 10 72 091 Manatí Arecibo ((-66.43895 18.48165, -66.43896 18.48116…
## # … with 68 more rows
# Combinar los datos de delitos con los datos geográficos de los distritos fiscales
deliMapa <- merge(distritos_fiscales, deliDF, by.x = "GROUP", by.y = "FISCALIA DISTRITO")
deliMapa
## Simple feature collection with 1872 features and 7 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -67.27135 ymin: 17.92688 xmax: -65.24442 ymax: 18.51576
## Geodetic CRS: WGS 84
## First 10 features:
## GROUP STATE COUNTY NAME Año Delito Casos
## 1 Aguadilla 72 071 Isabela 2020 Art2.8 29
## 2 Aguadilla 72 071 Isabela 2020 Art3.1 304
## 3 Aguadilla 72 071 Isabela 2020 Art3.2 94
## 4 Aguadilla 72 071 Isabela 2020 Art3.3 142
## 5 Aguadilla 72 071 Isabela 2020 Art3.4 2
## 6 Aguadilla 72 071 Isabela 2020 Art3.5 2
## 7 Aguadilla 72 071 Isabela 2022 Art2.8 40
## 8 Aguadilla 72 071 Isabela 2022 Art3.1 312
## 9 Aguadilla 72 071 Isabela 2023 Art2.8 6
## 10 Aguadilla 72 071 Isabela 2023 Art3.1 55
## geometry
## 1 POLYGON ((-67.10328 18.5134...
## 2 POLYGON ((-67.10328 18.5134...
## 3 POLYGON ((-67.10328 18.5134...
## 4 POLYGON ((-67.10328 18.5134...
## 5 POLYGON ((-67.10328 18.5134...
## 6 POLYGON ((-67.10328 18.5134...
## 7 POLYGON ((-67.10328 18.5134...
## 8 POLYGON ((-67.10328 18.5134...
## 9 POLYGON ((-67.10328 18.5134...
## 10 POLYGON ((-67.10328 18.5134...
# Coordenadas de los distritos fiscales de Puerto Rico
coordenadas_pr <- data.frame(
`FISCALIA DISTRITO` = c("Aguadilla", "Aibonito", "Arecibo", "Bayamón", "Caguas", "Carolina", "Fajardo", "Guayama", "Humacao", "Mayagüez", "Ponce", "San Juan", "Utuado", "Vega Baja"),
longitud = c(-67.1541, -66.2625, -66.7181, -66.1653, -66.0399, -65.9665,
-65.6522, -66.1109, -65.8329, -67.1457, -66.6168, -66.1057, -66.688, -66.4337),
latitud = c(18.4274, 18.142, 18.4503, 18.3798, 18.2343, 18.3802,
18.3258, 17.9756, 18.148, 18.201, 18.0108, 18.4655, 18.2635, 18.4461)
)
# Filtrar y fusionar datos
delitos_2020 <- subset(merge(deliDF, coordenadas_pr, by.x = "FISCALIA DISTRITO", by.y = "FISCALIA.DISTRITO"), Año == 2020 & Delito == "Art2.8")
# Crear una paleta de colores basada en el atributo deseado
colores <- colorFactor(
palette = c('#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf', '#aec7e8', '#ffbb78'), # Colores disponibles
domain = distritos_fiscales$GROUP # Atributo de las regiones agrícolas utilizado para generar la paleta de colores
)
# Crear el mapa Leaflet
m <- leaflet() %>%
addTiles(urlTemplate = "https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png") %>% # Agregar capa de teselas base
fitBounds(-67.5, 17.8, -65, 18.6) %>% # Ajustar límites del mapa
setView(-66.3, 18.2, zoom = 8.5) %>% # Establecer vista centrada en Puerto Rico
setMaxBounds(lng1 = -67.5, lat1 = 17.8, lng2 = -65, lat2 = 18.6) %>%
addPolygons(
data = distritos_fiscales, # Añadir polígonos de las regiones agrícolas
fillColor = ~colores(GROUP), # Asignar colores a cada región agrícola basados en el atributo STATE
color = "",
fillOpacity = 0.3,
weight = 2,
highlight = highlightOptions(
weight = 5, # Grosor del borde al resaltar
color = "white", # Color del borde al resaltar
fillOpacity = 0.7, # Opacidad del relleno al resaltar
)
) %>%
addMarkers(
lng = coordenadas_pr$longitud,
lat = coordenadas_pr$latitud,
popup = paste("Distrito:", coordenadas_pr$`FISCALIA DISTRITO`)
) # Añadir marcadores en los pueblos seleccionados
# Mostrar el mapa
m
# Seleccionar solo los datos para un tipo de delito específico
delito_seleccionado <- "Art3.5" # Reemplaza con el delito que deseas visualizar
año_seleccionado <- "2021"
datos_delito_seleccionado <- deliMapa %>%
filter(
Delito == delito_seleccionado,
Año == año_seleccionado
)
# Crear el objeto sf para los datos del delito seleccionado
datos_delito_seleccionado_sf <- st_as_sf(datos_delito_seleccionado)
# Crear el mapa con ggplot2
mapa_delitos <- ggplot() +
geom_sf(data = datos_delito_seleccionado_sf, aes(fill = Casos)) +
labs(title = paste("Incidencia de casos de", delito_seleccionado, "en el año", año_seleccionado)) +
scale_fill_gradient(name = "Incidencia de Casos", low = "lightblue", high = "darkblue") +
theme_minimal() +
theme(
legend.position = "bottom",
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank()
)
mapa_delitos
### interactividad
mapa_interactivo <- ggplotly(mapa_delitos,
tooltip = "text") %>%
style(
text = paste("Fiscalía:", datos_delito_seleccionado$GROUP, "<br>",
"Casos:", datos_delito_seleccionado$Casos, "<br>",
"Municipio:", datos_delito_seleccionado$NAME)
)
# Mostrar el mapa
mapa_interactivo